package A006Semaphore_CountDownLatch_CyclicBarrier;

import java.util.concurrent.CountDownLatch;

public class B005_AQS规范_CountDownLatch减少计数 {
    /**
     * 该类的构造方法为
     * CountDownLatch(int count)构造一个用给定计数初始化的CountDownLatch在这里插入代码片
     * <p>
     * 两个常用的主要方法
     * await() 使当前线程在锁存器倒计数至零之前一直在等待，除非线程被中断
     * countDown()递减锁存器的计数，如果计数达到零，将释放所有等待的线程
     */

    //6个同学陆续离开教室之后，班长锁门
    public static void main(String[] args) throws InterruptedException {

        //创建CountDownLatch对象，设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);

        //6个同学陆续离开教室之后
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 号同学离开了教室");

                //计数  -1
                countDownLatch.countDown();

            }, String.valueOf(i)).start();
        }

        //等待
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName() + " 班长锁门走人了");
    }
}
